package com.example.mapp.buildSql;

import com.example.ext.entity.ExtRepairInfo;
import org.apache.ibatis.jdbc.SQL;

/**
 * Created by youkun on 2017/10/24.
 */
public class RepairInfoMappSql {

    public String queryByElevatorList(ExtRepairInfo extRepairInfo){
        String select = " a.*, b.user_name as repairUserName, c.user_name as confirmUserName ";
        String table =" repair_info a left join sys_user b on a.repair_user_id=b.id left join sys_user c on a.confirm_user_id=c.id ";
        String whereParam="";
        if(extRepairInfo.getElevatorId()!=null && extRepairInfo.getElevatorId()>0){
            whereParam+=" and a.elevator_id = #{elevatorId}";
        }
        if(extRepairInfo.getElevatorNum()!=null && !extRepairInfo.getElevatorNum().equals("")){
            whereParam+=" and a.elevator_num = #{elevatorNum}";
        }
        if(extRepairInfo.getIsRepaired()!=null && !extRepairInfo.getIsRepaired().equals("")){
            whereParam+=" and a.is_repaired = #{isRepaired}";
        }
        if(extRepairInfo.getStartDate() != null && !extRepairInfo.getStartDate().equals("")){
            whereParam+=" and a.resolve_date >= #{startDate}";
        }
        if(extRepairInfo.getStartDate() != null && !extRepairInfo.getStartDate().equals("")){
            whereParam+=" and a.resolve_date <= #{endDate}";
        }

        String where = " 1=1 "+whereParam;
        String sql = new SQL(){{
            SELECT(select);
            FROM(table);
            WHERE(where);
            ORDER_BY("a.is_repaired ");
            ORDER_BY("a.id desc ");
        }
        }.toString();

        String limit = "";
        if(extRepairInfo.getCurrentPage() != null){
            int from = (extRepairInfo.getCurrentPage()-1)*extRepairInfo.getPageSize();
            limit = " limit "+from+","+extRepairInfo.getPageSize();
        }


        return sql+limit;
    }

    public String countByElevatorList(ExtRepairInfo extRepairInfo){
        String select = " count(a.id) ";
        String table =" repair_info a  ";
        String whereParam="";
        if(extRepairInfo.getElevatorId()!=null && extRepairInfo.getElevatorId()>0){
            whereParam+=" and a.elevator_id = #{elevatorId}";
        }
        if(extRepairInfo.getElevatorNum()!=null && !extRepairInfo.getElevatorNum().equals("")){
            whereParam+=" and a.elevator_num = #{elevatorNum}";
        }
        if(extRepairInfo.getIsRepaired()!=null && !extRepairInfo.getIsRepaired().equals("")){
            whereParam+=" and a.is_repaired = #{isRepaired}";
        }

        String where = " 1=1 "+whereParam;
        String sql =  new SQL(){{
            SELECT(select);
            FROM(table);
            WHERE(where);
        }
        }.toString();

        return sql;
    }
}
